"""Unit test for uglob.py"""

import os, shutil
import uglob
import unittest

class _Base(unittest.TestCase):
    def __init__( self, *args, **kwargs ):
        unittest.TestCase.__init__( self, *args, **kwargs )
        self.test_root = '/tmp/unittest/uglobtest'
        self._dirs = ['SUB1','SUB2']
        self._words = (
            'spam', 'eggs', 'foo', 'bar'
        )
    def get_filenames(self):
        for name in self._words:
            for dirname in self._dirs:
                for i in range( len( self._words ) ):
                    yield os.path.join( 
                        self.test_root, dirname, name
                    )+'.'+str(i+1)
            
    def setUp(self):
        if os.path.exists( self.test_root ):
            self.tearDown()
        for fn in self.get_filenames():
            d = os.path.dirname(fn)
            if not os.path.exists(d):
                os.makedirs( d )
            w = open( fn, 'w' )
            w.write( '# test file fo uglob.py: '+fn+'\n' )
            w.close()

    def tearDown(self):
        shutil.rmtree( self.test_root )

class UglobResult(_Base):
    '''Test uglob.uglob function.'''

    def test_with_under( self ):
        '''Returned paths must be relative to under.'''
        to_get = [
            'SUB1/spam.1',
            'SUB1/eggs.1',
            'SUB1/foo.1',
            'SUB1/bar.1',
        ]
        result = uglob.uglob( 'SUB1/*.1', self.test_root )
        self.assertEqual(set(to_get), set(result))  

    def test_without_under( self ):
        '''Returned paths must be absolute if no under given.'''
        to_get = self.get_filenames()
        result = uglob.uglob( os.path.join(self.test_root,'*/*') )
        self.assertEqual(set(to_get), set(result))  
        
        
        
        
if __name__ == "__main__":
    unittest.main()

